var api = require('../util/api.js');
import SignaturePad from '../util/signature_pad.js';
let signaturePad = {};
let pix = 7;
let penColor = 'black';
let lineWidth = 0.6;
Page({

  data: {
    penColor: 'black',
    lineWidth: 0.6,
    isEmpty: true
  },
  uploadScaleStart(e) {
    const item = {
      penColor: penColor,
      lineWidth: lineWidth
    };
    signaturePad._handleTouchStart(e, item);
  },
  uploadScaleMove(e) {
    signaturePad._handleTouchMove(e);
  },
  uploadScaleEnd: function (e) {
    signaturePad._handleTouchEnd(e);
    const isEmpty = signaturePad.isEmpty();
    this.setData({
      isEmpty: isEmpty
    })
  },
  retDraw: function () {
    signaturePad.clear();
    const isEmpty = signaturePad.isEmpty();
    this.setData({
      isEmpty: isEmpty
    })
  },
  onLoad: function (options) {
    var ctx = wx.createCanvasContext('handWriting');
    const data = {
      devicePixelRatio: pix,
    };
    signaturePad = new SignaturePad(ctx, data);

    let _this = this;
    let chain = api.chain();
    if (api.isBlank(chain.ljcyId)) {
      _this.setData({
        isEdit: false,
        item:{
          q1: '否',
          q2: '否',
          q3: '否',
          q4: '否',
          q5: '否',
          q6: '否',
          q7: '否',
          q8: '否',
          q9: '否',
          q10: '否'
        }
      });
    }else{
      api.get(`${api.url.ljcy}/${chain.ljcyId}`).then(res => {
        _this.setData({
          item: res.data,
          isEdit: true
        });
      })
    }
  },
  formSubmit: function (e) {
    const _this = this;
    let params = e.detail.value;
    let title = _this.checkParams(params);
    if(!api.isBlank(title)){
      wx.showToast({
        title: title,
        icon: 'none'
      });
      return false
    }
    wx.canvasToTempFilePath({
      canvasId: 'handWriting',
      success: function (res) {
        let tempFilePath = res.tempFilePath;
        api.uploadFile([tempFilePath], '', function (res) {
          params.signature = res[0].fileId;
          if(_this.data.isEdit){
            api.put(api.url.ljcy, params).then(res => {
              api.back("保存成功", "success");
            })
          }else{
            params.title = '廉洁回访问卷调查';
            api.post(api.url.ljcy, params).then(res => {
              api.back("保存成功", "success");
            });
          }
        });
      },
      fail: function (res) {
        console.log(res)
      },
      complete: function (res) {
        console.log(res)
      }
    })
  },
  checkParams(params){
    if (api.isBlank(params.superviseName)){
      return '姓名未填写';
    }
    if (api.isBlank(params.superviseYear)){
      return '年未填写';
    }
    if (api.isBlank(params.superviseMonth)){
      return '月未填写';
    }
    if (api.isBlank(params.superviseDay)){
      return '日未填写';
    }
    if (api.isBlank(params.superviseUnit)){
      return '施工方未填写';
    }
    if (api.isBlank(params.superviseWell)){
      return '井号未填写';
    }
    if (api.isBlank(params.superviseType)){
      return '驻井或巡井未填写';
    }
    if (api.isBlank(params.superviseState)){
      return '工况未填写';
    }
    if (this.data.isEmpty) {
      return '请签字';
    }
  }
})